package org.example.webserver.controller;

import com.alibaba.fastjson.JSONArray;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.webserver.pojo.Commodity;
import org.example.webserver.pojo.OrderState;
import org.example.webserver.pojo.User;
import org.example.webserver.service.CommodityService;
import org.example.webserver.service.OrderService;
import org.example.webserver.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Resource
    CommodityService commodityService;
    @Resource
    OrderService orderService;

    @PostMapping("/getMyComm")
    public Result<List<Commodity>> getMyComm(HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            return Result.success(commodityService.searchCommodityByMerchant(user.getUser()));
        }else{
            return Result.error("004","该用户没有权限");
        }
    }

    @PostMapping("/addComm")
    public Result<Commodity> addComm(@RequestBody Commodity commodity,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            if(commodity.getMerchant().equals(user.getUser())){
                return Result.success(commodityService.addCommodity(commodity));
            }else{
                return Result.error("005","登录信息失效");
            }
        }else{
            return Result.error("004","该用户没有权限");
        }
    }

    @PostMapping("/editComm")
    public Result<Commodity> editComm(@RequestBody Commodity commodity,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            if(commodity.getMerchant().equals(user.getUser())){
                return Result.success(commodityService.editCommodity(commodity));
            }else{
                return Result.error("005","登录信息失效");
            }
        }else{
            return Result.error("004","该用户没有权限");
        }
    }

    @PostMapping("/delComm")
    public Result<Commodity> delComm(@RequestBody Commodity commodity,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            if(commodity.getMerchant().equals(user.getUser())){
                commodityService.delCommodity(commodity);
                return Result.success();
            }else{
                return Result.error("005","登录信息失效");
            }
        }else{
            return Result.error("004","该用户没有权限");
        }
    }

    @PostMapping("/showOrder")
    public Result<JSONArray> showOrder(HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            JSONArray jsonArray=orderService.findOrderByMerchant(user.getUser());
            return Result.success(jsonArray);
        }else{
            return Result.error("004","该用户没有权限");
        }
    }

    @PostMapping("/send")
    public Result<OrderState> send(@RequestBody OrderState orderState,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(user!=null&&user.getUser_group().equals("merchant")){
            return Result.success(orderService.send(orderState));
        }else{
            return Result.error("004","该用户没有权限");
        }
    }
}
